import {cloneDeep,merge} from "lodash"
import {
    ADD_BASE_DEPARTMENT,
    CHANGE_ORGANIZES,
    CHANGE_ORGANIZES_NAME,
    GET_ALL_PERSON,
    ADD_ADMIN,
    REMOVE_MANAGER,
    DELETE_PERSON,
    ADD_PERSON,
    UPDATE_PERSON,
} from "../actions"

const initialState = {
    adding:false,
    organizes: {
        name:"",
        id:1,
        show:true,//展开收起
        layer:1,//层级
        children:[]
    },
    allPerson:[],//所有人员
}

const handlers = {
    [ADD_BASE_DEPARTMENT]: (state, data) => {
        state.organizes.name = data;
        return state;
    },
    [CHANGE_ORGANIZES]:(state,data) =>{
        let newState = cloneDeep(state);
        newState.organizes = data.organizes;
        newState.adding = data.adding;
        return newState;
    },
    [CHANGE_ORGANIZES_NAME]:(state,data)=>{
        let newState = cloneDeep(state);
        function changeName(organizes) {
            if(organizes.id === data.id){
                organizes.name = data.name;
                return false;
            }
            if(!!organizes.children.length){
                for(let z in organizes.children){
                    changeName(organizes.children[z])
                }
            }
        }
        changeName(newState.organizes);
        return newState;
    },
    [GET_ALL_PERSON]:(state,data)=>{
        let newState = cloneDeep(state);
        newState.allPerson = data;
        return newState;
    },
    [ADD_ADMIN]:(state,data)=> {
        let newState = cloneDeep(state);
        if(data.info instanceof Array){//是一个数组
            for (let x in newState.allPerson) {
                for(let y in data.info){
                    if (newState.allPerson[x].id == data.info[y]) {
                        newState.allPerson[x].manager = data.staffId
                    }
                }
            }
            console.info("xinz3",newState)
        }else{
            for (let x in newState.allPerson) {
                if (newState.allPerson[x].id == data.info.id) {
                    newState.allPerson[x].manager = data.staffId
                }
            }
        }
        return newState;
    },
    [REMOVE_MANAGER]:(state,data)=>{
        let newState = cloneDeep(state);
        for(let x in newState.allPerson){
            if(newState.allPerson[x].id == data.id){
                newState.allPerson[x].manager = "-1";
            }
        }
        return newState;
    },
    [DELETE_PERSON]:(state,data)=>{
        let newState = cloneDeep(state);
        for(let y in newState.allPerson){
            if(data.ids.indexOf(+newState.allPerson[y].id)>-1){
                newState.allPerson[y].assess = "";
            }
        }
        return newState;
    },
    [ADD_PERSON]:(state,data)=>{
        let newState = cloneDeep(state);
        console.info("add",data);
        newState.allPerson.concat(data);
        console.info("新增后",newState);
        return newState;
    },
    [UPDATE_PERSON]:(state,data)=>{
        let newState = cloneDeep(state);
        let person = newState.allPerson;
        let items = cloneDeep(person);
        for(let x in person){
            for(let z in data.person){
                if(data.person[z].id == person[x].id){
                    items[x] = data.person[z];
                }
            }
        }
        newState.allPerson = items;
        return newState;
    },
};

export default (state = initialState, action) => {
    const {type, data} = action;
    if (!handlers[type]) {return state}
    return handlers[type](state, data)
}
